package com.memo.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.memo.R;
import com.memo.model.Congratulation;

public class DAO extends OrmLiteBaseActivity<DatabaseHelper>{
	private static Activity activity;
	private static Dao<Congratulation, Integer> dao = null;
	private static final String TAG = "DAO";
	
	public static void insert(Activity act, Congratulation gift){
		activity = act;
		try {
			dao = getDBHelper().getCongratulationDao();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			dao.create(gift);
			act.finish();
			Toast.makeText(act, R.string.add_greating_saved, Toast.LENGTH_LONG).show();
			Log.i(TAG, "===== INSERT ====");
			Log.i(TAG, gift.toString());
		} catch (SQLException e) {					
			e.printStackTrace();
		}
	}
//-----------------------------------------------------------------------------------------	
	public static List<Congratulation> getGreatingsByDay(Activity act, Date date){
		activity = act;
		List<Congratulation> congs = null;
		Log.i(TAG, "=== TRY TO GET -->" + date.toString());
		try {
			dao = getDBHelper().getCongratulationDao();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		QueryBuilder<Congratulation, Integer> builder = dao.queryBuilder();
		
		try {
			builder.where().like(Congratulation.DATE, date);
			PreparedQuery<Congratulation> prepared = builder.prepare();
			congs = dao.query(prepared);
			Log.i(TAG, "==== GET ==== " + congs.size());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return congs;
	}
//-----------------------------------------------------------------------------------------	
	public static DatabaseHelper getDBHelper(){
		return new DatabaseHelper(activity);
	}
//----------------------------------------------------------------------------------------
	public static List<Congratulation> getGreatingsByMonth(Activity act, Calendar calendar){
		activity = act;
		List<Congratulation> congs = null;
		
		try {
			dao = getDBHelper().getCongratulationDao();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Date first = calendar.getTime();
		first.setDate(1);
		Date last = calendar.getTime();
		last.setDate(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        
        QueryBuilder<Congratulation, Integer> builder = dao.queryBuilder();
		try {
			builder.where().between(Congratulation.DATE, first, last);
			PreparedQuery<Congratulation> prepared = builder.prepare();
			congs = dao.query(prepared);
			Log.i(TAG, "==== GET ==== " + congs.size());
			for (Congratulation con : congs){
				Log.i(TAG, con.toString());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return congs;	
	}
//--------------------------------------------------------------------------------------
	/**
	 * 
	 * @return nearest event from current date
	 */
	public static Congratulation findFirstEven(){
		Congratulation cong = null;
		
		try {
			dao = getDBHelper().getCongratulationDao();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		QueryBuilder<Congratulation, Integer> builder = dao.queryBuilder();
		
		List<Congratulation> congs = null;
		try {
//			builder.where().like(Congratulation.DATE, date);
			PreparedQuery<Congratulation> prepared = builder.prepare();
			congs = dao.query(prepared);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		List<Date> dates = new ArrayList<Date>();
		for (Congratulation c : congs){
			dates.add(c.getDate());
		}
		
		final long now = System.currentTimeMillis();
		// Get date closest to "now"
		Date closest = Collections.min(dates, new Comparator<Date>() {
		    public int compare(Date d1, Date d2) {
		        long diff1 = Math.abs(d1.getTime() - now);
		        long diff2 = Math.abs(d2.getTime() - now);
		        return diff1 < diff2 ? -1 : 1;
		    }
		});
		
		return findCongByDate(congs, closest);
}
	private static Congratulation findCongByDate(List<Congratulation> congs, Date closest){
		Congratulation firstCong = null;
		for (Congratulation c : congs){
			if (c.getDate().getTime() == closest.getTime()){
				firstCong = c;
				break;
			}
		}
		return firstCong;
	}
}
